#include "mesh_gen.h"

// http://ozviz.wasp.uwa.edu.au/~pbourke/surfaces_curves/superellipse/

const int supermesh_sides = 16;

const float BYTE_TO_FLOAT = (1.0f / 255.0f);

struct Vertex {
	float x, y, z;
	float nx, ny, nz;
};

void gen_ring(const SuperMeshHeader *header, const SuperMeshRing *input, int idx, float z, Vec3 *vtx_out) {
	const int sides = 16;
	float angle_increment = (2 * PI) / sides;
	float angle = 0;
	for (int i = 0; i < sides; i++) {
		float hardness = 1.0f / (float)input->squareness;
		float width = input->xscale;
		float height = input->yscale;
		float x = pow(cos(angle), hardness) * width * BYTE_TO_FLOAT;
		float y = pow(sin(angle), hardness) * height * BYTE_TO_FLOAT;
		vtx_out->x = x;
		vtx_out->y = y;
		vtx_out->z = z;
		angle += angle_increment;
	}
}


// Returns a pointer to the next supermesh.

inline const SuperMeshHeader *gen_supermesh(const SuperMeshHeader *header) {
	// Rings are always stored directly behind the header.
	const SuperMeshRing *rings = (const SuperMeshRing *)(header + 1);
	// First, gen the vertices.
	Vec3 *vtx_out;
	for (int i = 0; i < header->ring_count; i++) {
		gen_ring(header, rings + i, i, z, 
		
	}
	// Compute memory location of next ring.
	return (const SuperMeshHeader *)(
		(const char *)header + sizeof(SuperMeshHeader) + header->ring_count * sizeof(SuperMeshRing));
}


void gen_all_supermesh(const SuperMeshReader *header) {
	while (true) {
		header = gen_supermesh(header);
	}
}
